package jp.wug.samples.cart;

import com.google.inject.Inject;
import com.google.inject.Provider;
import jp.wug.PageFlow;
import jp.wug.Renderer;

public class CartPage {
    Models.Cart cart;
    Provider<IndexPage> indexPage;
    Provider<BuyPage> buyPageProvider;
    Provider<PageFlow> pageFlowProvider;

    @Inject
    public CartPage(Models.Cart cart, Provider<IndexPage> indexPage, Provider<BuyPage> buyPageProvider, Provider<PageFlow> pageFlowProvider) {
        this.cart = cart;
        this.indexPage = indexPage;
        this.buyPageProvider = buyPageProvider;
        this.pageFlowProvider = pageFlowProvider;
    }

    public CartPage getCart() {
        return this;
    }

    public IndexPage getLinkToIndex() {
        return indexPage.get();
    }

    public void renderItems(Renderer<String, Models.CartItem> renderer) {
        for (Models.CartItem item : cart.getItems()) {
            renderer.render(item.getProduct().getKey(), item);
        }
    }

    public Models.CartItem getItem(String key) {
        return cart.getItem(key);
    }

    public void buy() {
        PageFlow pageFlow = pageFlowProvider.get();
        pageFlow.redirectTo(BuyPage.class, buyPageProvider.get());
    }
}
